
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Built-in class-based views API &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Base views" href="base.html" />
    <link rel="prev" title="System check framework" href="../checks.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="../checks.html" title="System check framework">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="base.html" title="Base views">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-class-based-views-index">
            
  <div class="section" id="s-built-in-class-based-views-api">
<span id="built-in-class-based-views-api"></span><h1>Built-in class-based views API<a class="headerlink" href="#built-in-class-based-views-api" title="Permalink to this headline">¶</a></h1>
<p>Class-based views API reference. For introductory material, see the
<a class="reference internal" href="../../topics/class-based-views/index.html"><span class="doc">Class-based views</span></a> topic guide.</p>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="base.html">Base views</a><ul>
<li class="toctree-l2"><a class="reference internal" href="base.html#view"><code class="docutils literal notranslate"><span class="pre">View</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="base.html#templateview"><code class="docutils literal notranslate"><span class="pre">TemplateView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="base.html#redirectview"><code class="docutils literal notranslate"><span class="pre">RedirectView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generic-display.html">Generic display views</a><ul>
<li class="toctree-l2"><a class="reference internal" href="generic-display.html#detailview"><code class="docutils literal notranslate"><span class="pre">DetailView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-display.html#listview"><code class="docutils literal notranslate"><span class="pre">ListView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generic-editing.html">Generic editing views</a><ul>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#formview"><code class="docutils literal notranslate"><span class="pre">FormView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#createview"><code class="docutils literal notranslate"><span class="pre">CreateView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#updateview"><code class="docutils literal notranslate"><span class="pre">UpdateView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-editing.html#deleteview"><code class="docutils literal notranslate"><span class="pre">DeleteView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="generic-date-based.html">Generic date views</a><ul>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#archiveindexview"><code class="docutils literal notranslate"><span class="pre">ArchiveIndexView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#yeararchiveview"><code class="docutils literal notranslate"><span class="pre">YearArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#montharchiveview"><code class="docutils literal notranslate"><span class="pre">MonthArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#weekarchiveview"><code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#dayarchiveview"><code class="docutils literal notranslate"><span class="pre">DayArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#todayarchiveview"><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="generic-date-based.html#datedetailview"><code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code></a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="mixins.html">Class-based views mixins</a><ul>
<li class="toctree-l2"><a class="reference internal" href="mixins-simple.html">Simple mixins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-simple.html#contextmixin"><code class="docutils literal notranslate"><span class="pre">ContextMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-simple.html#templateresponsemixin"><code class="docutils literal notranslate"><span class="pre">TemplateResponseMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-single-object.html">Single object mixins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-single-object.html#singleobjectmixin"><code class="docutils literal notranslate"><span class="pre">SingleObjectMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-single-object.html#singleobjecttemplateresponsemixin"><code class="docutils literal notranslate"><span class="pre">SingleObjectTemplateResponseMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-multiple-object.html">Multiple object mixins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-multiple-object.html#multipleobjectmixin"><code class="docutils literal notranslate"><span class="pre">MultipleObjectMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-multiple-object.html#multipleobjecttemplateresponsemixin"><code class="docutils literal notranslate"><span class="pre">MultipleObjectTemplateResponseMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-editing.html">Editing mixins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#formmixin"><code class="docutils literal notranslate"><span class="pre">FormMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#modelformmixin"><code class="docutils literal notranslate"><span class="pre">ModelFormMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#processformview"><code class="docutils literal notranslate"><span class="pre">ProcessFormView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-editing.html#deletionmixin"><code class="docutils literal notranslate"><span class="pre">DeletionMixin</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="mixins-date-based.html">Date-based mixins</a><ul>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#yearmixin"><code class="docutils literal notranslate"><span class="pre">YearMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#monthmixin"><code class="docutils literal notranslate"><span class="pre">MonthMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#daymixin"><code class="docutils literal notranslate"><span class="pre">DayMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#weekmixin"><code class="docutils literal notranslate"><span class="pre">WeekMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#datemixin"><code class="docutils literal notranslate"><span class="pre">DateMixin</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="mixins-date-based.html#basedatelistview"><code class="docutils literal notranslate"><span class="pre">BaseDateListView</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="flattened-index.html">Class-based generic views - flattened index</a><ul>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#simple-generic-views">Simple generic views</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#view"><code class="docutils literal notranslate"><span class="pre">View</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#templateview"><code class="docutils literal notranslate"><span class="pre">TemplateView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#redirectview"><code class="docutils literal notranslate"><span class="pre">RedirectView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#detail-views">Detail Views</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#detailview"><code class="docutils literal notranslate"><span class="pre">DetailView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#list-views">List Views</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#listview"><code class="docutils literal notranslate"><span class="pre">ListView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#editing-views">Editing views</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#formview"><code class="docutils literal notranslate"><span class="pre">FormView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#createview"><code class="docutils literal notranslate"><span class="pre">CreateView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#updateview"><code class="docutils literal notranslate"><span class="pre">UpdateView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#deleteview"><code class="docutils literal notranslate"><span class="pre">DeleteView</span></code></a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="flattened-index.html#date-based-views">Date-based views</a><ul>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#archiveindexview"><code class="docutils literal notranslate"><span class="pre">ArchiveIndexView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#yeararchiveview"><code class="docutils literal notranslate"><span class="pre">YearArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#montharchiveview"><code class="docutils literal notranslate"><span class="pre">MonthArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#weekarchiveview"><code class="docutils literal notranslate"><span class="pre">WeekArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#dayarchiveview"><code class="docutils literal notranslate"><span class="pre">DayArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#todayarchiveview"><code class="docutils literal notranslate"><span class="pre">TodayArchiveView</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="flattened-index.html#datedetailview"><code class="docutils literal notranslate"><span class="pre">DateDetailView</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="s-specification">
<span id="specification"></span><h2>Specification<a class="headerlink" href="#specification" title="Permalink to this headline">¶</a></h2>
<p>Each request served by a class-based view has an independent state; therefore,
it is safe to store state variables on the instance (i.e., <code class="docutils literal notranslate"><span class="pre">self.foo</span> <span class="pre">=</span> <span class="pre">3</span></code> is
a thread-safe operation).</p>
<p>A class-based view is deployed into a URL pattern using the
<a class="reference internal" href="base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> classmethod:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;view/&#39;</span><span class="p">,</span> <span class="n">MyView</span><span class="o">.</span><span class="n">as_view</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="mi">42</span><span class="p">)),</span>
<span class="p">]</span>
</pre></div>
</div>
<div class="admonition-thread-safety-with-view-arguments admonition">
<p class="first admonition-title">Thread safety with view arguments</p>
<p class="last">Arguments passed to a view are shared between every instance of a view.
This means that you shouldn’t use a list, dictionary, or any other
mutable object as an argument to a view. If you do and the shared object
is modified, the actions of one user visiting your view could have an
effect on subsequent users visiting the same view.</p>
</div>
<p>Arguments passed into <a class="reference internal" href="base.html#django.views.generic.base.View.as_view" title="django.views.generic.base.View.as_view"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_view()</span></code></a> will
be assigned onto the instance that is used to service a request. Using the
previous example, this means that every request on <code class="docutils literal notranslate"><span class="pre">MyView</span></code> is able to use
<code class="docutils literal notranslate"><span class="pre">self.size</span></code>. Arguments must correspond to attributes that already exist on
the class (return <code class="docutils literal notranslate"><span class="pre">True</span></code> on a <code class="docutils literal notranslate"><span class="pre">hasattr</span></code> check).</p>
</div>
<div class="section" id="s-base-vs-generic-views">
<span id="base-vs-generic-views"></span><h2>Base vs Generic views<a class="headerlink" href="#base-vs-generic-views" title="Permalink to this headline">¶</a></h2>
<p>Base class-based views can be thought of as <em>parent</em> views, which can be
used by themselves or inherited from. They may not provide all the
capabilities required for projects, in which case there are Mixins which
extend what base views can do.</p>
<p>Django’s generic views are built off of those base views, and were developed
as a shortcut for common usage patterns such as displaying the details of an
object. They take certain common idioms and patterns found in view
development and abstract them so that you can quickly write common views of
data without having to repeat yourself.</p>
<p>Most generic views require the <code class="docutils literal notranslate"><span class="pre">queryset</span></code> key, which is a <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code>
instance; see <a class="reference internal" href="../../topics/db/queries.html"><span class="doc">Making queries</span></a> for more information about <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code>
objects.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Built-in class-based views API</a><ul>
<li><a class="reference internal" href="#specification">Specification</a></li>
<li><a class="reference internal" href="#base-vs-generic-views">Base vs Generic views</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../checks.html"
                        title="previous chapter">System check framework</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="base.html"
                        title="next chapter">Base views</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/class-based-views/index.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="../checks.html" title="System check framework">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="base.html" title="Base views">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>